FUNCTION

text.regex_replace

function regex_replace(regex: text, replacement: text): text

Returns the text obtained by replacing all subsequences in this text which match the given regular expression regex, with the text replacement.

Supports named and numbered group references in the replacement text.

Examples:

  • 'aaaaacaaaaabaaaaacaaaaa'.regex_replace('(b|c)', 'd') returns 'aaaaadaaaaadaaaaadaaaaa'.
  • 'aaaaacaaaaabaaaaacaaaaa'.regex_replace('a+', '') returns 'cbc'.
  • 'johnsmith@chromaway.com'.regex_replace('[a-z]+', '0') returns '0@0.0'.
  • 'John Doe'.regex_replace('(\\w+) (\\w+)', '$2, $1') returns 'Doe, John'.
  • '2025-10-27'.regex_replace('(?<y>\\d{4})-(?<m>\\d{2})-(?<d>\\d{2})', '${d}/${m}/${y}') returns '27/10/2025'.

Parameters

  • regex — the regular expression to search with
  • replacement — the replacement text

Returns

text equal to this, but with regions that match regex replaced with replacement

Throws

  • exception — if regex is not a valid regular expression

Since

0.14.16

© 2026 Chromia